package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @aothor LentDent1
 * @vision 1.0
 */
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        // 创建一个Setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 将数据存入数据库
        setmealMapper.insertMeal(setmeal);

        // 获取生成套餐的id
        Long id = setmeal.getId();

        // 获取套餐的dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            // 设置dish的id 为什么？ 因为在存入数据库的时候需要套餐id 归属
            setmealDish.setId(id);
        });

        // 将套餐dish存入数据库 保存套餐和菜品的联系
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });

        ids.forEach(setmealId -> {
            // 删除套餐表中对应的数据
            setmealMapper.deleteById(setmealId);
            // 删除套餐菜品表中对应的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }
    @Override
    public SetmealVO selectById(Long id) {

        // 根据id查询setmeal表
        Setmeal setmeal = setmealMapper.selectById (id);
        // 根据id查询setmeal_dish表
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);

        // 将查询到的数据封装成一个SetmealVO表
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        // 返回
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {

        // 封装为Setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 修改套餐表
        setmealMapper.update(setmeal);

        // 获取套餐id
        Long id = setmealDTO.getId();

        // 删除之前对应dish的套餐id
        setmealDishMapper.deleteBySetmealId(id);

        // 添加现在套餐内对应dish的setmealid
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {

            setmealDish.setDishId(id);
        });

        // 添加到setmealDish表
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 套餐起售 停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id ) {
        // 如果想要操作的套餐里面含有停售dish，则报错：将要起售的套餐中含有停售dish
        if (status == StatusConstant.ENABLE)  {
            // 调用mapper接口获取套餐，获取dishesList
            // select a.* from dish a left join setmeal_dish b
            // on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId (id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
